package com.bangdream.michelia.tool.retrofithttp;

import android.util.Log;

import com.bangdream.michelia.BuildConfig;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.tool.retrofithttp.converter.UrlAddInterceptor;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.SPUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Mr.Wang on 2017/9/27.
 * 909732184@qq.com
 */

public class RetroFactory {
    public static String baseUrl = ApiService.baseUrl;
    private static Request request = null;

    private RetroFactory() {

    }

    //定制OkHttp 可以设置缓存 超时实际 请求头之类的数据
    private static OkHttpClient httpClient = OKhttpManager.getInstance().getOkHttpClient();


    private static OkHttpClient httpClient2;

    private static ApiService retrofitService = null;
    //private static ApiService retrofitUserService  = null;


    public static ApiService getInstance() {
        if (retrofitService == null) {
            baseUrl = ApiService.baseUrl;
            request = new Request.Builder()
                    //.addHeader("cookie", AppCurrentUser.getInstance().getUserCookie())
                    .url(baseUrl)
//                    .addHeader("Content-Type", "application/json; charset=UTF-8")
                    .build();

            UrlAddInterceptor urlAddInterceptor = new UrlAddInterceptor.Builder()
                    .addQueryParam("domain", MainApplication.getInstance().getDomain())
//                    .addQueryParam("token",AppCurrentUser.getInstance().getToken())
                    .build();
            if (httpClient2 == null) {
                HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                OkHttpClient.Builder builder = new OkHttpClient.Builder()
                        .addInterceptor(urlAddInterceptor)
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS);
                if(BuildConfig.DEBUG){
                    builder.addInterceptor(loggingInterceptor);
                }
                httpClient2=builder.build();
            }

            httpClient2.newCall(request);
            retrofitService = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    //Retrofit与FastJson混用  //Retrofit与Gson混用GsonConverterFactory  FastJsonConverterFactory
                    .addConverterFactory(GsonConverterFactory.create(new Gson()))
                    //rx与retrofit混用 将retrofit原本返回的call转换为Observable
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(httpClient2)
                    .build()
                    .create(ApiService.class);
        }

        return retrofitService;
    }
//    //用户服务的Retrofit实例
//    public static ApiService getUserServiceInstance() {
//        if(retrofitUserService==null) {
//            baseUrl = ApiService.baseUserUrl;
//            request = new Request.Builder()
//                    //.addHeader("cookie", AppCurrentUser.getInstance().getUserCookie())
//                    .url(baseUrl)
//                    .build();
//            httpClient2.newCall(request);
//            retrofitUserService = new Retrofit.Builder()
//                    .baseUrl(baseUrl)
//                    //Retrofit与FastJson混用  //Retrofit与Gson混用GsonConverterFactory FastJsonConverterFactory
//                    .addConverterFactory(GsonConverterFactory.create(new Gson()))
//                    //rx与retrofit混用 将retrofit原本返回的call转换为Observable
//                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .client(httpClient2)
//                    .build()
//                    .create(ApiService.class);
//        }
//        return retrofitUserService;
//    }
}
